﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmItem : Form
    {
        DialogResult dlgResult;
        ItemInfo itemInfo;

        public frmItem(int _item_ID)
        {
            InitializeComponent();

            itemInfo = new ItemInfo();
            UISetting.LoadOrigin(ref cboOrigin);
            UISetting.LoadCalculationUnit(ref cboCalculationUnit);
            txtCode.Text = UISetting.AutoFillItemCode();
            if (_item_ID > 0)
            {
                itemInfo = ItemDB.GetInfo(_item_ID);
               
                InfoToForm();
            }
            itemInfo.Item_ID = _item_ID;
            
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            
            itemInfo.Item_Code = txtCode.Text;
            itemInfo.Item_Name = txtName.Text;
            itemInfo.Item_OriginID = Convert.ToInt32(cboOrigin.SelectedValue);
            itemInfo.Item_CalculationUnitID = Convert.ToInt32(cboCalculationUnit.SelectedValue);
            itemInfo.Item_PurchasePrice = Convert.ToDecimal(txtPurchasePrice.Text);
            itemInfo.Item_WholesalePrice = Convert.ToDecimal(txtWholesalePrice.Text);
            itemInfo.Item_RetailPrice = Convert.ToDecimal(txtRetailPrice.Text);
        }
        private void InfoToForm()
        {
            txtCode.Text = itemInfo.Item_Code;
            txtName.Text = itemInfo.Item_Name;
            cboOrigin.SelectedValue = Convert.ToInt32(itemInfo.Item_OriginID);
            cboCalculationUnit.SelectedValue = Convert.ToInt32(itemInfo.Item_CalculationUnitID);

            txtPurchasePrice.Text = Convert.ToDecimal(itemInfo.Item_PurchasePrice).ToString("#,##0");
            txtWholesalePrice.Text = Convert.ToDecimal(itemInfo.Item_WholesalePrice).ToString("#,##0");
            txtRetailPrice.Text = Convert.ToDecimal(itemInfo.Item_RetailPrice).ToString("#,##0");
        }
        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên mặt hàng trống! Hãy nhập lại.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }
            if (!CheckTextValid.IsNumeric(txtPurchasePrice.Text))
            {
                MessageBox.Show("Giá mua không hợp lệ! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPurchasePrice.Focus();
                txtPurchasePrice.Text = "0.0";
                return false;
            }
            if (!CheckTextValid.IsNumeric(txtWholesalePrice.Text))
            {
                MessageBox.Show("Giá bán buôn không hợp lệ! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtWholesalePrice.Focus();
                txtWholesalePrice.Text = "0.0";
                return false;
            }
            if (!CheckTextValid.IsNumeric(txtRetailPrice.Text))
            {
                MessageBox.Show("Giá bán lẻ không hợp lệ! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRetailPrice.Focus();
                txtRetailPrice.Text = "0.0";
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            TextContentDecimalNoChanged();
            FormToInfo();
            if (itemInfo.Item_ID > 0)
            {
                if (ItemDB.Exist(itemInfo.Item_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (ItemDB.Update(itemInfo))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (ItemDB.Exist(itemInfo.Item_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                itemInfo.Item_ID = ItemDB.Insert(itemInfo);
                if (itemInfo.Item_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Create_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            itemInfo.Item_ID = 0;
            txtCode.Text = UISetting.AutoFillItemCode();
            txtName.Text = "";
            cboOrigin.SelectedIndex = 0;
            cboCalculationUnit.SelectedIndex = 0;
            txtPurchasePrice.Text = "0.0";
            txtWholesalePrice.Text = "0.0";
            txtRetailPrice.Text = "0.0";
        }

        private void frmItem_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.F2:
                    btnSaveToNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

        private void txtPurchasePrice_Leave(object sender, EventArgs e)
        {
            txtPurchasePrice.Text = UISetting.GetDecimalNo(txtPurchasePrice.Text).ToString("#,##0");
            TextContentDecimalNoChanged();
        }

        private void txtWholesalePrice_Leave(object sender, EventArgs e)
        {
            txtWholesalePrice.Text = UISetting.GetDecimalNo(txtWholesalePrice.Text).ToString("#,##0");
        }

        private void txtRetailPrice_Leave(object sender, EventArgs e)
        {
            txtRetailPrice.Text = UISetting.GetDecimalNo(txtRetailPrice.Text).ToString("#,##0");
        }

        void TextContentDecimalNoChanged()
        {
            if (itemInfo.Item_ID > 0)
                return;

            decimal pricedeifference = CompanyDB.GetInfo(1).Company_PriceDifference;
            decimal dec = 0;
            if (!Decimal.TryParse(txtPurchasePrice.Text, out dec) || dec < 0)
                txtWholesalePrice.Undo();
            else
                txtWholesalePrice.Text = (dec + dec * pricedeifference / 100).ToString("#,##0");

            if (!Decimal.TryParse(txtPurchasePrice.Text, out dec) || dec < 0)
                txtRetailPrice.Undo();
            else
                txtRetailPrice.Text = (dec + dec * (pricedeifference + pricedeifference) / 100).ToString("#,##0");
        }
        private void btnSaveToNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
            btnNew_Click(null, null);
        }
    }
}